//MIT License
//
//Copyright(c) 2020 Goodman Tao
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files(the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions :
//
//The above copyright notice and this permission notice shall be included in all
//copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE.


#ifdef _MSC_VER
#define _LB_2_MULTITHREAD_API __declspec(dllexport)
#endif // _MSC_VER

#ifdef __GNUC__
#define _LB_2_MULTITHREAD_API
#endif // __GNUC__


#include "_LB_2_Multithread.h"

#include <stdlib.h>
#include "tinycthread.h"


typedef struct
{
    mtx_t *pMtx;
} Fld;

static void Clear(void *pParams)
{
    Mutex *pThis = pParams;
    Fld *pFld = pThis->Fld;

    mtx_destroy(pFld->pMtx);
    free(pFld->pMtx);
}

static void Init(void *_pThis, va_list *pvlArgs)
{
    Mutex *pThis = _pThis;

    MutexType type = va_arg(*pvlArgs, MutexType);
    ConcurrenceReturnValue *pRetVal = va_arg(*pvlArgs, ConcurrenceReturnValue *);

    Fld *pFld = pThis->Fld;

    pFld->pMtx = malloc(sizeof(mtx_t));
    ConcurrenceReturnValue ret = mtx_init(pFld->pMtx, type);
    if (pRetVal)
    {
        *pRetVal = ret;
    }
}

static void Lock(void *_pThis, va_list *pvlArgs)
{
    Mutex *pThis = _pThis;
    
    ConcurrenceReturnValue *pRetVal = va_arg(*pvlArgs, ConcurrenceReturnValue *);

    Fld *pFld = pThis->Fld;

    ConcurrenceReturnValue ret = mtx_lock(pFld->pMtx);
    if (pRetVal)
    {
        *pRetVal = ret;
    }
}

static void TimedLock(void *_pThis, va_list *pvlArgs)
{
    Mutex *pThis = _pThis;

    const struct timespec *ts = va_arg(*pvlArgs, void *);
    ConcurrenceReturnValue *pRetVal = va_arg(*pvlArgs, ConcurrenceReturnValue *);

    Fld *pFld = pThis->Fld;

    ConcurrenceReturnValue ret = mtx_timedlock(pFld->pMtx, ts);
    if (pRetVal)
    {
        *pRetVal = ret;
    }
}

static void TryLock(void *_pThis, va_list *pvlArgs)
{
    Mutex *pThis = _pThis;

    ConcurrenceReturnValue *pRetVal = va_arg(*pvlArgs, void *);

    Fld *pFld = pThis->Fld;

    ConcurrenceReturnValue ret = mtx_trylock(pFld->pMtx);
    if (pRetVal)
    {
        *pRetVal = ret;
    }
}

static void Unlock(void *_pThis, va_list *pvlArgs)
{
    Mutex *pThis = _pThis;

    ConcurrenceReturnValue *pRetVal = va_arg(*pvlArgs, void *);

    Fld *pFld = pThis->Fld;

    ConcurrenceReturnValue ret = mtx_unlock(pFld->pMtx);
    if (pRetVal)
    {
        *pRetVal = ret;
    }
}

__CONSTRUCTOR(Mutex)
{
    return __Inst(__TYPE(Mutex), sizeof(Fld), Clear, 5, 0,
        __METHOD(Init),
        __METHOD(Lock),
        __METHOD(TimedLock),
        __METHOD(TryLock),
        __METHOD(Unlock));
}
